<template>
  <div>
    <div style="display: flex;justify-content: space-between;margin: 5px 0">
      <el-input placeholder="请输入权限名" v-model.trim="search" style="margin-bottom: 5px;width: 300px">
        <el-button type="primary" slot="append" icon="el-icon-search" @click="getData"></el-button>
      </el-input>
      <span>
        <el-button size="small" type="primary" style="margin-right: 70px" @click="addPermission">添加权限</el-button>
      </span>
    </div>
    <el-table
      :data="tableData" style="width: 100%;margin-bottom: 20px;" border>
      <el-table-column prop="id" label="ID" min-width="50" align="center"></el-table-column>
      <el-table-column prop="description" label="权限名称" min-width="100" align="center"></el-table-column>
      <el-table-column prop="authority" label="授权字符串" min-width="200" align="center"></el-table-column>
      <el-table-column prop="url" label="请求路径" min-width="200" align="center"></el-table-column>
      <el-table-column label="操作" min-width="120" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogType===0?'新增':'编辑'" :visible.sync="dialog" width="500px" @close="closeDialog">
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="authority" label="路径" :rules="[{ required: true, message: '请输入请求路径', trigger: 'blur'}]">
          <el-input v-model="form.url" placeholder="请求路径"></el-input>
        </el-form-item>
        <el-form-item prop="authority" label="授权" :rules="[{ required: true, message: '请输入授权字符串', trigger: 'blur'}]">
          <el-input v-model="form.authority" placeholder="授权字符串"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="名称" :rules="[{ required: true, message: '请输入名称', trigger: 'blur'}]">
          <el-input v-model="form.description" placeholder="授权名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "PermissionManager",
  data() {
    return {
      search: '',
      tableData: [],
      dialog: false,
      dialogType: '',
      form: {
        id: '',
        url: '',
        authority: '',
        description: ''
      }
    }
  }, methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let url = this.dialogType === 0 ? '/permission/addPermission' : '/permission/editPermission';
          this.$axios.post(url, this.$qs.stringify(this.form)).then(res => {
            if (res.code === 200) {
              this.$message({type: 'success', duration: 1000, message: '提交成功!'});
              this.getData();
              this.closeDialog();
            }
          });
        }
      })
    },
    addPermission() {
      this.dialogType = 0;
      this.dialog = true;
    },
    handleEdit(row) {
      this.dialog = true;
      this.dialogType = 1;
      this.form = {id: row.id, url: row.url, authority: row.authority, description: row.description}
    },
    handleDelete(row) {
      this.$confirm('确认删除?').then(() => {
        this.$axios.post('/permission/deletePermission', this.$qs.stringify({id: row.id})).then(res => {
          if (res.code === 200) {
            this.$message({type: 'success', duration: 1000, message: '删除成功!'});
            this.getData();
          }
        });
      }).catch(() => {
      });
    },
    closeDialog() {
      this.dialog = false;
      this.dialogType = '';
      this.form = {id: '', url: '', authority: '', description: ''};
      this.$refs['form'].resetFields();//清除验证
    },
    getData() {
      this.$axios.post('/permission/findAllPermission', this.$qs.stringify({name: this.search})).then(res => {
          if (res.code === 200) {
            this.tableData = res.data;
          }
        }
      );
    }
  }, created() {
    this.getData();
  }
}
</script>

<style scoped>

</style>
